/**
 * 封装一个获取元素函数
 */
function $ (select) {
	return document.querySelector(select);
}

function $$ (select) {
	return document.querySelectorAll(select);
}

//添加特定类名
function addClass (ele, className) {
	ele.classList.add(className);
}

//删除特定类名
function removeClass (ele, className) {
	ele.classList.remove(className);
}

function log (str) {
	console.log(str);
}

//完美运动框架
function startMove(obj, json, fn) {
    clearInterval(obj.timer);

    obj.timer = setInterval(function (){
	    var bStop = true;  //这一次运动就结束了——所有的值都到达了
	                    
	    for(var attr in json) {

		    //1.取当前的值
		    var iCur = 0;
		   
		    if(attr == 'opacity') {
		       iCur = parseInt(parseFloat(getStyle(obj, attr))*100);
		    } else {
		       iCur = parseInt(getStyle(obj, attr));
		    }
		   
		    //2.算速度
		    var iSpeed = (json[attr]-iCur)/10;
		    
		    iSpeed = iSpeed > 0?Math.ceil(iSpeed):Math.floor(iSpeed);
		   
		   //3.检测停止
		   if(iCur!=json[attr]) {
		       bStop=false;
		    }
		   
		   if(attr=='opacity') {
		       obj.style.filter='alpha(opacity:'+(iCur+iSpeed)+')';
		       obj.style.opacity=(iCur+iSpeed)/100;
		   } else {
		       obj.style[attr]=iCur+iSpeed+'px';
		   }

	    }
		if(bStop) {
		   clearInterval(obj.timer);
		   
		   if(fn){
		      fn();
		   }
	    }
 	}, 13)
}

function getByClass(oParent, sClass) {
    var aEle=document.getElementsByTagName('*');
    var i=0;
    var aResult=[];
    
    for(i=0;i<aEle.length;i++)
    {
        if(aEle[i].className==sClass)
        {
            aResult.push(aEle[i]);
        }
    }
    return aResult;
}


function getStyle(obj, attr) {

    if(obj.currentStyle){
        return obj.currentStyle[attr];
    }
    else {
        return getComputedStyle(obj,false)[attr];
    }
}

/**
 * Get the move.使元素横向移动，高度随机变化
 *
 * @param      {<type>}  obj     The obj要移动的元素
 * @param      {string}  speed   The speed移动的速度值，取数字
 */
 function getMove (obj, stage, speed) {
    var bigWidth = stage.offsetWidth + obj.offsetWidth - 10,
    	height = stage.offsetHeight - obj.offsetHeight,
    	objWidth = obj.offsetWidth,
        speed = Math.random()*speed,
        arr = [];

        //初始化
        obj.style.left = bigWidth + "px";

    //移动函数
    function move () {
        var oldLeft = obj.offsetLeft;
            // ranHeight;

        if (oldLeft > -objWidth) {
            obj.style.left = oldLeft - speed  + "px";
            requestAnimationFrame(move);
        } else {
            obj.style.left = bigWidth + "px";

         
            obj.style.top =Math.random()*height + "px";

            requestAnimationFrame(move);
        }   
    }
    requestAnimationFrame(move);
}

/**
 * ajax封装函数
 *
 * @return     {Object}  { 一个ajax函数封装类,仿jquery的调用方式 }
 */
function ajax (options) {
    options = options || {};
    options.type = (options.type || "GET").toUpperCase();
    options.dataType = options.dataType || "json";
    var params = formatParams(options.data);

    //创建
    if (window.XMLHttpRequest) {
        var xhr = new XMLHttpRequest();
    } else {
        var xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }

    //接收
    xhr.onreadystatechange = function  () {
        if (xhr.readyState == 4) {
            var status = xhr.status;
            if (status >= 200 && status < 300) {
                options.success && options.success(xhr.responseText);
            } else {
                options.fail && options.fail(status);
            }
        }
    }

    //连接发送
    if (options.type == "GET") {
        xhr.open("GET", options.url+"?"+params, true);
        xhr.send(null);
    } else if (options.type == "POST") {
        xhr.open("POST", options.url, true);

        //设置表单提交时内容的类型
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhr.send(params);
    }

    //格式化参数
    function formatParams (data) {
        var arr = [];
        for (var name in data) {
            arr.push(encodeURIComponent(name)+"="+encodeURIComponent(data[name]));
        }
        arr.push(("v="+Math.random()).replace(".", ""));
        return arr.join("&");
    }

}
